package com.xsomnus.java_basic.aqs;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @author 叶小夏
 * @since 2019-08-26 08:58
 */
public class AQSTest {

    private static Lock lock = new ReentrantLock();
    private static int counter = 0;

    private static class EchoTimeThread extends Thread {

        public EchoTimeThread(String name) {
            super(name);
        }

        @Override
        public void run() {
            try {
                lock.lock();
                System.out.println(Thread.currentThread().getId() + "=>" + LocalDateTime.now().format(DateTimeFormatter.ISO_DATE_TIME) + "| val =>" + counter++);
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            } finally {
                lock.unlock();
            }

        }
    }


    public static void main(String[] args) throws InterruptedException {

        for (int i = 0; i < 3; i++) {
            new EchoTimeThread("tt" + i).start();
        }


        System.out.println(" " + Thread.currentThread().getId() + "=>" + LocalDateTime.now().format(DateTimeFormatter.ISO_DATE_TIME) + "| val =>" + counter);
    }

}
